package com.study.d030animation;

import javafx.animation.Animation.Status;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;

/**时间线动画
*@description
*@author xtayhame
*date 2020年3月22日
*version
*since v1.0
*/
public class TimelineMain extends Application{
	public static void main(String[] args) {
		launch(args);
	}
	@Override
	public void start(Stage stage) throws Exception {
		AnchorPane anchorPane = new AnchorPane();
		HBox hBox = new HBox(20);
		Button b1=new Button("播放");
		Button b2=new Button("暂停");
		Button b3=new Button("停止");
		Button b4=new Button("设置加速");
		Button b7=new Button("从第二个点开始");

		Button b5=new Button("移动");

		Rectangle rectangle = new Rectangle(300, 300,Paint.valueOf("#EE82EE55"));

		hBox.getChildren().addAll(b1,b2,b3,b4,b7);
		anchorPane.getChildren().addAll(hBox,b5,rectangle);
		AnchorPane.setTopAnchor(b5, 80.0);
		AnchorPane.setLeftAnchor(b5, 100.0);

		AnchorPane.setTopAnchor(rectangle, 80.0);
		AnchorPane.setLeftAnchor(rectangle, 100.0);

		stage.setScene(new Scene(anchorPane, 800, 600));
		stage.show();

		//时间线动画
		Timeline timeline = new Timeline();
		//从（0,0）点开始
		KeyFrame kf1 = new KeyFrame(Duration.seconds(0),"kf1", new EventHandler<ActionEvent>(){
			@Override
			public void handle(ActionEvent event) {
				System.out.println("kf1--x.");
			}

		},new KeyValue(b5.translateXProperty(),0));

		KeyFrame kf2 = new KeyFrame(Duration.seconds(0),"kf2", new EventHandler<ActionEvent>(){
			@Override
			public void handle(ActionEvent event) {
				System.out.println("kf1--y.");
			}

		},new KeyValue(b5.translateYProperty(),0));


		//5秒到(300,0)
		KeyFrame kf3 = new KeyFrame(Duration.seconds(5), "kf3",new EventHandler<ActionEvent>(){
			@Override
			public void handle(ActionEvent event) {
				System.out.println("kf3.");
			}

		},new KeyValue(b5.translateXProperty(),300));
		KeyFrame kf4 = new KeyFrame(Duration.seconds(5), "kf3",new EventHandler<ActionEvent>(){
			@Override
			public void handle(ActionEvent event) {
				System.out.println("kf3.");
			}

		},new KeyValue(b5.translateYProperty(),0));


		//10秒后到(400,400)的位置
		KeyFrame kf5 = new KeyFrame(Duration.seconds(10),"kf4", new EventHandler<ActionEvent>(){
			@Override
			public void handle(ActionEvent event) {
				System.out.println("kf4.");
			}

		},new KeyValue(b5.translateXProperty(),300));

		KeyFrame kf6 = new KeyFrame(Duration.seconds(10),"kf5", new EventHandler<ActionEvent>(){
			@Override
			public void handle(ActionEvent event) {
				System.out.println("kf5.");
			}

		},new KeyValue(b5.translateYProperty(),300));



		timeline.getKeyFrames().addAll(kf1,kf2,kf3,kf4,kf5,kf6);

	/*	//设置自动反转播放,前提是setCycleCount()大于1,才有效果，即自动从后向前播放
		timeline.setAutoReverse(true);
		//设置播放次数  Timeline.INDEFINITE:无限次
		timeline.setCycleCount(Timeline.INDEFINITE);
		//设置延迟时间
		timeline.setDelay(Duration.seconds(2));
		//设置速率    即以几倍的速度播放
		timeline.setRate(3);*/


		//设置播放完成后 ，前提是setCycleCount()不能是无限次
		timeline.setOnFinished(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				System.out.println("播放完毕");
			}
		});

		//监听状态
		timeline.statusProperty().addListener(new ChangeListener<Status>() {
			@Override
			public void changed(ObservableValue<? extends Status> observable, Status oldValue, Status newValue) {
				System.out.println("当前状态：" + newValue);
			}
		});




		b1.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				//开始播放
				timeline.play();

				System.out.println("总的播放时间："+timeline.getTotalDuration().toSeconds());
			}
		});

		b2.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				//暂停
				timeline.pause();

			}
		});


		b3.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				//停止
				timeline.stop();
			}
		});

		//设置
		b4.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				//加速
				timeline.setRate(2);
			}
		});

		b7.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				//从2秒钟的地方重新开始
				//timeline.jumpTo(Duration.seconds(2));

				//从kf3这个点重新开始
				//timeline.jumpTo("kf3");

				//从2秒钟的地方重新开始
				timeline.playFrom(Duration.seconds(2));

				//从kf3这个点重新开始
				//timeline.playFrom("kf3");

				//从头开始
				timeline.playFromStart();
			}
		});
	}



}
